package com.ipzoe.web.controller.cms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.cms.domain.Case;
import com.ipzoe.cms.service.ICaseService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 案例Controller
 *
 * @author eric
 * @date 2020-08-15
 */
@RestController
@RequestMapping("/cms/case" )
public class CaseController extends BaseController {

    @Autowired
    private ICaseService iCaseService;

    /**
     * 查询案例列表
     */
    @PreAuthorize("@ss.hasPermi('cms:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(Case caseEntity)
    {
        startPage();
        LambdaQueryWrapper<Case> lqw = new LambdaQueryWrapper<Case>();
        if (StringUtils.isNotBlank(caseEntity.getName())){
        lqw.like(Case::getName,caseEntity.getName());
    }
        if (caseEntity.getCreateTime() != null){
        lqw.eq(Case::getCreateTime ,caseEntity.getCreateTime());
    }
        List<Case> list = iCaseService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出案例列表
     */
    @PreAuthorize("@ss.hasPermi('cms:case:export')" )
    @Log(title = "案例" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Case caseEntity) {
        LambdaQueryWrapper<Case> lqw = new LambdaQueryWrapper<Case>(caseEntity);
        List<Case> list = iCaseService.list(lqw);
        ExcelUtil<Case> util = new ExcelUtil<Case>(Case. class);
        return util.exportExcel(list, "case" );
    }

    /**
     * 获取案例详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:case:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCaseService.getById(id));
    }

    /**
     * 新增案例
     */
    @PreAuthorize("@ss.hasPermi('cms:case:add')" )
    @Log(title = "案例" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Case caseEntity) {
        return toAjax(iCaseService.save(caseEntity) ? 1 : 0);
    }

    /**
     * 修改案例
     */
    @PreAuthorize("@ss.hasPermi('cms:case:edit')" )
    @Log(title = "案例" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Case caseEntity) {
        return toAjax(iCaseService.updateById(caseEntity) ? 1 : 0);
    }

    /**
     * 删除案例
     */
    @PreAuthorize("@ss.hasPermi('cms:case:remove')" )
    @Log(title = "案例" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCaseService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
